package redis

import (
	"fmt"
	"github.com/go-redis/redis"
	"week2a/appconfig"
	"week2a/models/global"
)

func InitRedis() {
	err := global.Err
	res := appconfig.AppData.Redis
	//单例模式
	global.Once.Do(func() {
		global.RedisDB = redis.NewClient(&redis.Options{
			Addr:     fmt.Sprintf("%s:%d", res.Host, res.Port),
			Password: "", // no password set
			DB:       0,  // use default DB
		})
		_, err = global.RedisDB.Ping().Result()
	})

	if err != nil {
		fmt.Println("缓存链接失败", err)
		return
	}
	fmt.Println("缓存链接成功", res)
	// Output: PONG <nil>
}
